package com.workstudy.web.controller.workstudy;

import java.util.List;

import com.workstudy.common.core.domain.entity.SysUser;
import com.workstudy.system.service.ISysUserService;
import com.workstudy.workstudy.domain.WorkstudyAttendance;
import com.workstudy.workstudy.domain.WorkstudyWageVo;
import com.workstudy.workstudy.service.IWorkstudyAttendanceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.workstudy.common.annotation.Log;
import com.workstudy.common.enums.BusinessType;
import com.workstudy.workstudy.domain.WorkstudyWage;
import com.workstudy.workstudy.service.IWorkstudyWageService;
import com.workstudy.common.core.controller.BaseController;
import com.workstudy.common.core.domain.AjaxResult;
import com.workstudy.common.utils.poi.ExcelUtil;
import com.workstudy.common.core.page.TableDataInfo;

/**
 * 工资Controller
 * 
 * @author ws
 * @date 2021-03-28
 */
@Controller
@RequestMapping("/workstudy/wage")
public class WorkstudyWageController extends BaseController
{
    private String prefix = "workstudy/wage";

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IWorkstudyWageService workstudyWageService;

    @Autowired
    private IWorkstudyAttendanceService workstudyAttendanceService;

    @RequiresPermissions("workstudy:wage:view")
    @GetMapping()
    public String wage()
    {
        return prefix + "/wage";
    }

    /**
     * 查询工资列表
     */
    @RequiresPermissions("workstudy:wage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkstudyWage workstudyWage)
    {
        startPage();
        List<WorkstudyWage> list = workstudyWageService.selectWorkstudyWageList(workstudyWage);
        return getDataTable(list);
    }

    /**
     * 导出工资列表
     */
    @RequiresPermissions("workstudy:wage:export")
    @Log(title = "工资", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkstudyWage workstudyWage)
    {
        List<WorkstudyWage> list = workstudyWageService.selectWorkstudyWageList(workstudyWage);
        ExcelUtil<WorkstudyWage> util = new ExcelUtil<WorkstudyWage>(WorkstudyWage.class);
        return util.exportExcel(list, "wage");
    }

    /**
     * 新增工资
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //查询学生
        List<SysUser> userList = userService.selectUserByroleKeys("student");
        mmap.put("students", userList);
        return prefix + "/add";
    }

    /**
     * 新增保存工资
     */
    @RequiresPermissions("workstudy:wage:add")
    @Log(title = "工资", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkstudyWage workstudyWage,String attendanceIds)
    {
        if("".equals(attendanceIds)){
            return error("请选择要结算的考勤记录！");
        }
        return toAjax(workstudyWageService.insertWorkstudyWage(workstudyWage,attendanceIds));
    }

    /**
     * 修改工资
     */
    @GetMapping("/edit/{wageId}")
    public String edit(@PathVariable("wageId") Long wageId, ModelMap mmap)
    {
        WorkstudyWage workstudyWage = workstudyWageService.selectWorkstudyWageById(wageId);
        mmap.put("workstudyWage", workstudyWage);
        return prefix + "/edit";
    }

    /**
     * 修改保存工资
     */
    @RequiresPermissions("workstudy:wage:edit")
    @Log(title = "工资", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkstudyWage workstudyWage)
    {
        return toAjax(workstudyWageService.updateWorkstudyWage(workstudyWage));
    }

    /**
     * 删除工资
     */
    @RequiresPermissions("workstudy:wage:remove")
    @Log(title = "工资", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(workstudyWageService.deleteWorkstudyWageByIds(ids));
    }

    /**
     * 新增工资
     */
    @RequiresPermissions("workstudy:wage:info")
    @Log(title = "工资", businessType = BusinessType.UPDATE)
    @GetMapping("/info/{wageId}")
    public String info(@PathVariable("wageId") Long wageId,ModelMap mmap)
    {
        WorkstudyWageVo workstudyWageVo=new WorkstudyWageVo();
        //查询学生
        WorkstudyWage workstudyWage = workstudyWageService.selectWorkstudyWageById(wageId);
        workstudyWageVo.setWageId(workstudyWage.getWageId());
        workstudyWageVo.setUserId(workstudyWage.getUserId());
        workstudyWageVo.setSumprice(workstudyWage.getSumprice());
        workstudyWageVo.setSumduration(workstudyWage.getSumduration());
        workstudyWageVo.setSettlementBy(workstudyWage.getSettlementBy());
        workstudyWageVo.setSettlementTime(workstudyWage.getSettlementTime());
        //查询考勤详情
        List<WorkstudyAttendance> workstudyAttendanceList = workstudyAttendanceService.selectWorkstudyAttendanceBywageId(wageId);
        workstudyWageVo.setWorkstudyAttendanceList(workstudyAttendanceList);
        mmap.put("workstudyWageVo", workstudyWageVo);
        return prefix + "/info";
    }
}
